package org.openharmony.qttool.classgenerator;

import org.openharmony.qttool.Utils;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

public class QtClassGenerator {
    private final String className;

    private final String baseName;

    private final String saveTo;
    public QtClassGenerator(String className, String baseName, String saveTo) {
        this.className = className;
        this.baseName = baseName;
        this.saveTo = saveTo;
    }

    public boolean generate() {
        if (className == null || className.isEmpty()
                || baseName == null || baseName.isEmpty()
                || saveTo == null || saveTo.isEmpty())
            return false;

        String headerMacro = className.toUpperCase() + "_H";
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("%BASENAME%", baseName);
        hashMap.put("%HEADER_MARCO%", headerMacro);
        hashMap.put("%CLASSNAME%", className);
        hashMap.put("%HEADER%", className.toLowerCase() + ".h");
        hashMap.put("%PARENTNAME%", baseName);

        String pluginPath = Utils.pluginPath();
        Path headerFile = Paths.get(pluginPath, "template/class/class.h");
        Path cppFile = Paths.get(pluginPath, "template/class/class.cpp");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("/").append(className.toLowerCase()).append(".h");
        Path saveHeaderFile = Paths.get(saveTo, stringBuilder.toString());
        if (!Utils.readAndSaveToFile(headerFile, saveHeaderFile, hashMap))
            return false;
        stringBuilder = new StringBuilder();
        stringBuilder.append("/").append(className.toLowerCase()).append(".cpp");
        Path saveCppFile = Paths.get(saveTo, stringBuilder.toString());
        Utils.readAndSaveToFile(cppFile, saveCppFile, hashMap);
//        Toto add to cmakelists.txt
        return true;
    }
}
